﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace DAL
{
    public class ClientService
    {
        public static Client AddClient(Client client)
        {
            string sql = string.Format("insert into Client(PersonalInformationId) values({0});select @@IDENTITY;", client.PersonalInformationId);
            int id = DBHelper.GetScalar(sql);
            return GetClientById(id);
        }

        public static int GetPersonalInformationIdById(int id)
        {
            Client client = GetClientById(id);
            if (client == null)
                return -1;
            else
                return client.PersonalInformationId;
        }

        public static Client GetClientById(int id)
        {
            string sql = string.Format("select * from Client where Id={0}", id);
            return GetClientBySql(sql);
        }

        private static Client GetClientBySql(string sql)
        {
            List<Client> list = GetClientsBySql(sql);
            if (list.Count() > 0)
                return list[0];
            return null;
        }

        private static List<Client> GetClientsBySql(string sql)
        {
            List<Client> list = new List<Client>();
            DataTable dt = DBHelper.GetDataSet(sql);
            foreach (DataRow row in dt.Rows){
                Client client = new Client();
                client.Id = (int)row["Id"];
                client.PersonalInformationId = (int)row["PersonalInformationId"];
                list.Add(client);
            }
            return list;
        }
    }
}
